package com.hottv.meeting.videomeeting.module.edp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.chinamobile.iot.onenet.edp.CmdMsg;
import com.chinamobile.iot.onenet.edp.Common;
import com.chinamobile.iot.onenet.edp.EdpMsg;
import com.chinamobile.iot.onenet.edp.PushDataMsg;
import com.chinamobile.iot.onenet.edp.SaveDataMsg;
import com.chinamobile.iot.onenet.edp.toolbox.EdpClient;
import com.chinamobile.iot.onenet.edp.toolbox.Listener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.module.edp.data.ContentItem;
import com.hottv.meeting.videomeeting.module.edp.data.EdpMsgItem;
import com.hottv.meeting.videomeeting.module.edp.data.MeetingInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by chengwenqiang on 2019/6/4.
 */

public class EDPListener implements Listener {

    private Context mContext;


    public EDPListener(Context mContext)
    {
        this.mContext = mContext;
    }

    @Override
    public void onReceive(List<EdpMsg> msgList) {

        if (null == msgList) {
            return;
        }
        for (EdpMsg msg : msgList) {
            if (null == msg) {
                continue;
            }
            EDPService.mCurrent = System.currentTimeMillis();
//            Log.v(EDPConfig.TAG, "onReceive----------------------------------type:"+msg.getMsgType());
            switch (msg.getMsgType()) {
                // 连接响应

                case Common.MsgType.CONNRESP:
                    Log.v(EDPConfig.TAG, "连接响应");

                    //Toasty.info(mContext, "连接响应").show();
                    break;

                // 心跳响应
                case Common.MsgType.PINGRESP:
                    //Toasty.info(mContext, "心跳响应").show();
                    //LogUtils.recordHeartBeat();
                    Log.v(EDPConfig.TAG, "心跳响应");
                    break;
                // 存储确认
                case Common.MsgType.SAVERESP:
                    Log.v(EDPConfig.TAG, "SAVERESP 存储确认");

                    //Toasty.info(mContext, "存储确认").show();
                    break;
                // 转发（透传）
                case Common.MsgType.PUSHDATA:
                    PushDataMsg pushData = (PushDataMsg)msg;

                    String msgContent = new String(pushData.getData());
                    Log.v(EDPConfig.TAG, "PUSHDATA 转发（透传）"+msgContent);


                    Intent intent = new Intent();
                    intent.putExtra(MeetingConfig.MeetingDATA_Incall, msgContent);
                    ComponentName comp = new ComponentName("com.hottv.meeting.videomeeting","com.hottv.meeting.videomeeting.IncallActivity");
                    intent.setComponent(comp);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    mContext.startActivity(intent);

                    //Toasty.info(mContext, "转发（透传）").show();
                    break;
                // 存储（转发）
                case Common.MsgType.SAVEDATA:
                    SaveDataMsg saveDataMsg = (SaveDataMsg) msg;
                    List<byte[]> dataList = saveDataMsg.getDataList();
                    StringBuffer sb = new StringBuffer();
                    for (byte[] bytes : dataList) {
                        sb.append(new String(bytes));
                    }

                    String strContent = sb.toString();
                    Log.v(EDPConfig.TAG, "SAVEDATA 存储（转发）===" + strContent);



                    //Toasty.info(mContext, "存储（转发）===").show();
                    break;
                // 命令请求
                case Common.MsgType.CMDREQ:
                    CmdMsg cmdMsg = (CmdMsg) msg;
                    Log.v(EDPConfig.TAG, "cmdid: " + cmdMsg.getCmdId() + "\n命令请求内容: " + new String(cmdMsg.getData()));
                    //Toasty.info(mContext, "cmdid: " + cmdMsg.getCmdId() + "\n命令请求内容: " + new String(cmdMsg.getData())).show();
                    String jsonMsg = new String(cmdMsg.getData());
                    EdpClient.getInstance().sendCmdResp(cmdMsg.getCmdId(), new String("{\"rsmsg\":{\"rsdesc\":\"成功\",\"rscode\":\"0\"}}").getBytes());
                    //                    handleCommand(jsonMsg);
                    break;
                // 加密响应
                case Common.MsgType.ENCRYPTRESP:
                    Log.v(EDPConfig.TAG, "加密响应");
                    break;
            }
        }
    }

    @Override
    public void onFailed(Exception e) {
        e.printStackTrace();
        Log.v(EDPConfig.TAG, "连接失败！");
    }

    @Override
    public void onDisconnect() {
        //ToastUtils.show(mContext, "失去连接");
        Log.v(EDPConfig.TAG, "失去连接");
        try {
            EdpClient.getInstance().connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
